package com.nstar.uecenter.view.main.impl
{
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.util.LoadXMLDelegate;
	import com.nstar.base.util.Logger;
	import com.nstar.uecenter.view.components.ProductLoader;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.containers.ViewStack;
	import mx.controls.Button;
	import mx.events.FlexEvent;
	import mx.rpc.IResponder;

	public class UECenterClass extends VBox implements IResponder
	{
		private var loader:LoadXMLDelegate ;
		private var logger:Logger = Logger.getInstance();
		
		
		//public var picList:Canvas;
		//public var coverflow:CoverFlowContainer;
		public var vsList:ViewStack;
		[Bindable]
		public var uecProducts:Array = new Array();
		
		public var btnPrior:Button;
		public var btnNext:Button;
		
		public function UECenterClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			loadProduct();
			
			btnPrior.addEventListener(MouseEvent.CLICK,priorHandler);
			btnNext.addEventListener(MouseEvent.CLICK,nextHandler);
			
			this.addEventListener(MouseEvent.MOUSE_WHEEL,wheelHandler);
			vsList.addEventListener("productDetails",showProduct);
			
		}
		
		//前一个
		private function priorHandler(event:MouseEvent):void{
			var idx:int = vsList.selectedIndex;
			idx--;
			if(idx<0){
				idx = vsList.numChildren-1;
			}
			
			vsList.selectedIndex = idx;
		}
		//下一个
		private function nextHandler(event:MouseEvent):void{
			var idx:int = vsList.selectedIndex;
			idx++;
			if(idx>vsList.numChildren-1){
				idx = 0;
			}
			vsList.selectedIndex = idx;
		}
		//鼠标中间
		private function wheelHandler(event:MouseEvent):void{
			if(event.delta<0){
				nextHandler(null);
			}else{
				priorHandler(null);
			}
		}
		
		//加载产品数据
		private function loadProduct():void{
			loader = new LoadXMLDelegate(this,"demo/data/products.xml","object");
			loader.load();
		}
		
		public function result( rpcEvent : Object ) : void
		{
			// call the helper class for parse the XML data
			var products:ArrayCollection = rpcEvent.result.products.product;
			addProducts(vsList,products);
			uecProducts = products.toArray();
			trace("load products ok!");
		}
		
		// this is called when the delegate receives a fault from the service
		public function fault( rpcEvent : Object ) : void 
		{
			trace("load products error!!!");
		}
		
		//添加产品
		private function addProducts(container:ViewStack,products:ArrayCollection,index:int = 0):void{
			
		}
		
		//显示产品详情
		private function showProduct(event:AdvanceEvent):void{
			var p:Point = this.localToGlobal(new Point());
			ProductLoader.show(event.request,p.x,p.y,this.width,this.height);
		}
		
	}
}